<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of reserva_model
 *
 * @author Neil
 */
class reserva_model extends CI_Model {

    public function habitaciones($est) {
        $query = $this->db->query('select h.id_habi, h.nro_habi, th.desc_tipo, th.tarifa, h.piso  
                                    from habitacion h inner join tipo_habitacion th
                                    on h.tipo_habi = th.id_tipo where est_habi =' . $est);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }
        return $data;
    }

    public function clientesRegistrados($nombre) {
        if ($nombre == "nombre" || $nombre == "vacio") {
            $query = $this->db->query('select r.idRegistro,c.nombre, c.dni,h.nro_habi,r.fec_ing,p.nom_per from cliente c inner join registro r
                                    on c.cod_cli=r.cliente inner join personal p
                                    on p.id_pers = r.personal inner join habitacion h
                                    on r.habitacion = h.id_habi where est_reg = 1');
        } else {
            $query = $this->db->query("select r.idRegistro,c.nombre, c.dni,h.nro_habi,r.fec_ing,p.nom_per from cliente c inner join registro r
                                    on c.cod_cli=r.cliente inner join personal p
                                    on p.id_pers = r.personal inner join habitacion h
                                    on r.habitacion = h.id_habi where est_reg = 1 and c.nombre like '%$nombre%'");
        }

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function detalleRegistro($id) {
        $query = $this->db->query('select dr.id_det,p.cod_prod,p.desc_prod,p.pre_prod,dr.cantidad,dr.sub_total, dr.fechaDet from producto p inner join detalle_registro dr
                                on p.cod_prod = dr.producto inner join registro r
                                on dr.registro = r.idRegistro where registro=?', $id);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function regReserva($datos) {
        $this->db->insert('reserva', $datos);
    }

    //se creo nueva funcion, BETSY
    public function NotificacionesReserva() {


        $query = $this->db->query("SELECT t.desc_tipo,r.id_res,r.fec_entrada, r.est_res,r.fec_res,r.tel_res,r.nom_res,r.res_agen
FROM reserva r
INNER JOIN habitacion h ON r.habitacion = h.id_habi
INNER JOIN tipo_habitacion t ON t.id_tipo = h.tipo_habi
WHERE est_res=1 AND fec_res
BETWEEN NOW( ) 
AND DATE_ADD( NOW( ) , INTERVAL 4 
DAY ) ");
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    
    
    //se creo nueva funcion, BETSY
    
    
       public function listReserva($fec1,$fec2) {
        
            if ($fec2 == "") {
                $query= $this->db->query("SELECT t.desc_tipo,r.id_res,r.fec_entrada, r.est_res,r.fec_res,r.tel_res,r.nom_res,r.res_agen
FROM reserva r INNER JOIN habitacion h ON r.habitacion = h.id_habi 
INNER JOIN tipo_habitacion t ON t.id_tipo = h.tipo_habi where r.fec_res = '$fec1'");
            }else{
                $query= $this->db->query("SELECT t.desc_tipo,r.id_res,r.fec_entrada, r.est_res,r.fec_res,r.tel_res,r.nom_res,r.res_agen
FROM reserva r INNER JOIN habitacion h ON r.habitacion = h.id_habi 
INNER JOIN tipo_habitacion t ON t.id_tipo = h.tipo_habi where r.fec_res between '$fec1' and '$fec2'");
            }
            
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }
    
    public function NotificacionesReservaHOY() {

        
        $query = $this->db->query('SELECT t.desc_tipo,r.id_res,r.fec_entrada, r.est_res,r.fec_res,r.tel_res,r.nom_res,r.res_agen
FROM reserva r INNER JOIN habitacion h ON r.habitacion = h.id_habi 
INNER JOIN tipo_habitacion t ON t.id_tipo = h.tipo_habi
WHERE est_res=1 and fec_res = CURDATE();');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function regDetalle($datos) {
        $this->db->insert('detalle_registro', $datos);

        $query = $this->db->query('SELECT max(id_det) as codDet FROM detalle_registro');
        return $query->result_array();
    }

    public function eliminarProdDetalle($cod) {
        $this->db->delete('detalle_registro', array('id_det' => $cod));
    }

    //funcion agregada
    public function CancelarReserva($codigo, $data) {
        $this->db->where('id_res', $codigo);
        $this->db->update('reserva', $data);
    }

    public function finalizar($id, $data) {
        $this->db->where('idRegistro', $id);
        $this->db->update('registro', $data);
    }

}

?>
